﻿using System;
using System.Linq;
using MvcMall.AdminWeb.DtoModels;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.MvcPager;

namespace MvcMall.AdminWeb.Manage.Menu
{
    public partial class Index : ManageBasePage
    {
        private IAdminMenuTask _adminMenuTask;
        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("AdminMenuIndex");
            _adminMenuTask = GetInstance<IAdminMenuTask>();
            if (!IsPostBack)
            {
                BindRpt();
            }
        }

        private void BindRpt()
        {
            var list = _adminMenuTask.GetPagedList(ANPager.CurrentPageIndex, ANPager.PageSize);
            var dtoList = list.Select(m => new AdminMenuDto
                                               {
                                                   Id = m.Id,
                                                   LinkUrl = m.LinkUrl,
                                                   MenuName = m.MenuName,
                                                   ModulName = m.AdminModul.ModulName,
                                                   SortOrder = m.SortOrder
                                               })
                .ToPagedList(ANPager.CurrentPageIndex, ANPager.PageSize, list.TotalItemCount);
            rptMenus.DataSource = dtoList;
            rptMenus.DataBind();
            ANPager.RecordCount = dtoList.TotalItemCount;
        }

        protected void ANPager_PageChanged(object sender, EventArgs e)
        {
            BindRpt();
        }
    }
}